/*global pi, document, self */
// 按钮模块
// 按钮必须有bt_name属性，bt_name属性的值是作为样式的名字前缀，
// 通过设置className来改变按钮，按钮样式为名字前缀+(_normal, xxx_over, xxx_press, xxx_selected, xxx_disable, xxx_prompt),
// 按钮上的函数名称优先查找forelet上的方法，其次查找并执行当前window上的方法(属性为bt_click_func, bt_click_arg)
// 按钮支持激发点击事件时自动改变选中状态的标志(属性为click_select和dbclick_select)
// click可以支持点击后延时一段时间不响应click事件(属性为click_delay)
//按钮可以放置其他元素

pi.mod.define('pi.ui.button', function(pi) {
	"use strict";
	// 定义模块
	var module = {},
	getButton = function(button) {
		return pi.lang.isString(button) ? document.getElementById(button) : button;
	},
	// 设置按钮样式，修改class
	setStyle = function(b) {
		if (b.getAttribute("enabled") == "false") {
			pi.html.setClass(b, b.getAttribute("bt_name") + "_disable");
		} else if (b.getAttribute("selected") == "true") {
			pi.html.setClass(b, b.getAttribute("bt_name") + "_selected");
		} else if (b.getAttribute("prompt") == "true") {
			pi.html.setClass(b, b.getAttribute("bt_name") + "_prompt");
		} else {
			pi.html.setClass(b, b.getAttribute("bt_name") + "_normal");
		}
	},
	buttonHandler = function(e) {
		var target, func, arg, forelet;
		// 不是左键事件则返回
		if (e.button !== 0) {
			return;
		}
		// 获取事件对象
		target = pi.html.getElement(e.target, "bt_name");
		if (!target) {
			return;
		}
		// 按钮被禁用则返回
		if (target.getAttribute("enabled") == "false") {
			return false;
		}
		// 确定事件类型
		switch (e.type) {
		case "mousedown":
			// 被选中的按钮不再响应该操作
			if (target.getAttribute("selected") == "true") {
				return false;
			}
			func = target.getAttribute("bt_mousedown_func");
			arg = target.getAttribute("bt_mousedown_arg");
			pi.html.setClass(target, target.getAttribute("bt_name") + "_press");
			break;
		case "mouseup":
			if (target.getAttribute("selected") == "true") {
				return false;
			}
			func = target.getAttribute("bt_mouseup_func");
			arg = target.getAttribute("bt_mouseup_arg");
			setStyle(target);
			break;
		case "mouseover":
			if (target.getAttribute("selected") == "true") {
				return false;
			}
			func = target.getAttribute("bt_mouseover_func");
			arg = target.getAttribute("bt_mouseover_arg");
			pi.html.setClass(target, target.getAttribute("bt_name") + "_over");
			break;
		case "mouseout":
			if (target.getAttribute("selected") == "true") {
				return false;
			}
			func = target.getAttribute("bt_mouseout_func");
			arg = target.getAttribute("bt_mouseout_arg");
			setStyle(target);
			break;
		case "click":
			// 提供点击延时设置，在延时时间内，不处理点击事件
			arg = target.getAttribute("click_delay");
			if (arg) {
				arg = pi.lang.parseIntUnsafe(arg) + pi.lang.parseIntUnsafe(target.getAttribute("click_delay_time"));
				forelet = pi.lang.createDate().getTime();
				if (forelet < arg) {
					return false;
				}
				target.setAttribute("click_delay_time", forelet);
			}
			if (target.getAttribute("click_select") == "true") {
				target.setAttribute("selected", target.getAttribute("selected") == "true");
			}
			func = target.getAttribute("bt_click_func");
			arg = target.getAttribute("bt_click_arg");
			setStyle(target);
			break;
		case "dblclick":
			if (target.getAttribute("dbclick_select") == "true") {
				target.setAttribute("selected", target.getAttribute("selected") == "true");
			}
			func = target.getAttribute("bt_dblclick_func");
			arg = target.getAttribute("bt_dblclick_arg");
			pi.html.setClass(target, target.getAttribute("bt_name") + "_normal");
			break;
		default:
			return;
		}
		// 优先查找forelet上的方法，其次查找并执行当前window上的方法
		forelet = e.forelet;
		if (!forelet) {
			return;
		}
		if (func) {
			forelet.exec(func, [arg, target, e]);
		}
		return false;
	};

	// 设置按钮可用状态
	module.setEnabled = function(button, enabled) {
		var el = getButton(button);
		if (el) {
			el.setAttribute("enabled", enabled);
			setStyle(el);
		}
	};
	// 设置按钮选择状态
	module.setSelected = function(button, selected) {
		var el = getButton(button);
		if (el) {
			el.setAttribute("selected", selected);
			setStyle(el);
		}
	};
	// 设置按钮提示状态
	module.setPrompt = function(button, prompt) {
		var el = getButton(button);
		if (el) {
			el.setAttribute("prompt", prompt);
			setStyle(el);
		}
	};
	// 创建按钮事件处理器
	module.bind = function(handlerTable) {
		handlerTable.mousedown.add(buttonHandler);
		handlerTable.mouseup.add(buttonHandler);
		handlerTable.mouseover.add(buttonHandler);
		handlerTable.mouseout.add(buttonHandler);
		handlerTable.click.add(buttonHandler);
		handlerTable.dblclick.add(buttonHandler);
	};

	return module;
});